import time
from selenium.webdriver.common.by import By


class  Bank_cardOperation:

###################################    银行卡操作前后   ###################################
    def bank_card_before(self,driver,username,password):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(1)
        num1 = self.cardnumber(driver)
        return num1

    def bank_card_after(self,driver):
        return self.cardnumber(driver)

#########################################################################################
###################################     新增银行卡      ###################################
    def bank_card_add(self,driver,cardname,cardtype,cardnum):
        # 点击新增
        driver.find_element(By.XPATH, "//*[@id='bankCard_add_modal_btn']").click()
        time.sleep(1)
        # 输入银行卡所属银行
        driver.find_element(By.XPATH, "//input[@id='cardBank_add_input']").send_keys(cardname)
        time.sleep(1)
        # 选择银行卡类别
        if cardtype == "借记卡":
            # 点击借记卡
            driver.find_element(By.XPATH,
                                "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[1]").click()
        else:
            # 点击信用卡
            driver.find_element(By.XPATH,
                                "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]").click()
        time.sleep(1)
        # 输入银行卡号
        cardnum = f"{int(cardnum)}"
        driver.find_element(By.XPATH, "//input[@id='cardNum_add_input']").send_keys(cardnum)
        time.sleep(1)
        # 保存
        driver.find_element(By.XPATH, "//*[@id='bankCard_save_btn']").click()
        time.sleep(1)

#########################################################################################
###################################     删除卡测试      ###################################

    def bank_card_del(self,driver,num1,expect):
        driver.find_element(By.XPATH,
                            f"/html/body/div/div/main/div/div/div[{num1}]/div/div/div[1]/ul/li/button[2]").click()
        time.sleep(1)
        if expect == "成功":
            # 点击确定
            driver.find_element(By.XPATH,
                                f"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        else:
            # 点击取消
            driver.find_element(By.XPATH,
                                f"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]").click()
        time.sleep(1)

#########################################################################################
###################################     编辑银行卡      ###################################

    def bank_card_edit(self,driver,cardname,cardtype,cardnum,num1):
        # 最后一张银行卡点击编辑
        global flag
        driver.find_element(By.XPATH,
                            f"/html/body/div/div/main/div/div/div[{num1}]/div/div/div[1]/ul/li/button[1]").click()
        time.sleep(1)
        # 编辑银行卡名称
        if cardname != "":
            # 编辑银行卡名称
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[2]/form/div[1]/div[1]/input").clear()
            time.sleep(1)
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[2]/form/div[1]/div[1]/input").send_keys(
                cardname)
            flag = "1"
        # 编辑银行卡类别
        elif cardtype == "借记卡":
            # 编辑银行卡类别
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span").click()
            flag = "2"
            time.sleep(1)
        elif cardtype == "信用卡":
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span").click()
            flag = "2"
        # 编辑银行卡卡号
        elif cardnum != "":
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[2]/form/div[3]/div[1]/input").clear()
            time.sleep(1)
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[2]/form/div[3]/div[1]/input").send_keys(
                cardnum)
            flag = "3"
        else:
            print("没有获取到修改信息")
            # 点击关闭
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[3]/button[2]").click()
        time.sleep(1)
        try:
            # 点击更新
            driver.find_element(By.XPATH,
                                f"/html/body/div[1]/div/main/div/div/div[{num1+3}]/div/div/div[3]/button[2]").click()
        except:
            num1 = "0"
        time.sleep(1)
        num2 = self.cardnumber(driver)
        if flag != "0":
            if flag == "1":
                num1 = cardname
                num2 = driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div[{num2}]/div/div/div[1]/div/h4").text[:-6]
            if flag == "2":
                num1 = cardtype
                num2 = driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div[{num2}]/div/div/div[1]/div/h4").text[-4:-1]
            if flag == "3":
                num1 = cardnum
                num2 = driver.find_element(By.XPATH,f"/html/body/div/div/main/div/div/div[{num2}]/div/div/div[2]/h4[1]").text[3:]
            else:
                num2 = num1
        return num1,num2

###################################    获取实际结果     ###################################
    def getResult1(self,num1,num2):

        if num1 == num2:
            result = "失败"
        else:
            result = "成功"
        return result

    #  获取实际结果
    def getResult2(self,num1,num2):

        if num1 == num2:
            result = "成功"
        else:
            result = "失败"
        return result

################################### 获取账号内银行卡数量 ###################################
    # 获取账号内银行卡数量
    def cardnumber(self,driver):
        # 获取当前页面银行卡数量
        tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div")
        time.sleep(1)
        divs = tbody.find_elements(By.XPATH, "div")
        num = len(divs) - 3
        time.sleep(2)
        return num
